package a06_栈与队列;

import java.util.Stack;

/**
 * <p>
 * a01_用栈实现队列
 * </p>
 *
 * @author flyduck
 * @since 2024-07-25
 */
public class a02_用栈实现队列复习1 {

    public static void main(String[] args) {
        //["MyQueue","push","push","push","push","pop","push","pop","pop","pop","pop"]
        MyQueue2 myQueue2 = new MyQueue2();
        myQueue2.push(1);
        myQueue2.push(2);
        myQueue2.push(3);
        myQueue2.push(4);
        myQueue2.pop();
        myQueue2.push(5);
        myQueue2.pop();
        myQueue2.pop();
        myQueue2.pop();
        myQueue2.pop();
    }

}

class MyQueue2 {

    private Stack<Integer> stackIn;
    private Stack<Integer> stackOut;

    public MyQueue2() {
        stackIn = new Stack<>();
        stackOut = new Stack<>();
    }

    public void push(int x) {
        stackIn.push(x);
    }

    public int pop() {
        if(!stackOut.isEmpty()){
            return stackOut.pop();
        }else {
            while (!stackIn.isEmpty()) {
                stackOut.push(stackIn.pop());
            }
            return stackOut.pop();
        }
    }

    public int peek() {
        if(!stackOut.isEmpty()){
            return stackOut.peek();
        }else {
            while (!stackIn.isEmpty()) {
                stackOut.push(stackIn.pop());
            }
            return stackOut.peek();
        }
    }

    public boolean empty() {
        return stackIn.isEmpty() && stackOut.isEmpty();
    }
}
